<?php
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);

function curl_requests($url, $data = array(), $headers = array())
{
// 1. 初始化
    $ch = curl_init();
// 2. 设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    if ($data) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
//        print_r($url)."<br>";
    }

    $output = curl_exec($ch);

    if ($output === FALSE) {
        // print_r( curl_getinfo($ch) );
        echo curl_error($ch);
    }else{
        list ( $resHeaders ,  $body )  =  explode ( "\r\n\r\n" ,  $output ,  2 ) ;
        $ret[] = $resHeaders;
        $ret[] = $body;
        return $ret;
    }
    // 4. 释放curl句柄
    curl_close($ch);
    return FALSE;
}

function login($username, $passwd)
{
    $url = "https://wangzi.uk/auth/login";
    //设置post数据
    $query_data = array(
        "email" => $username,
        "passwd" => $passwd,
        "code" => ""
    );
    $host = "wangzi.uk";
//    $cookies = "expire_in=1691521441; uid=22206; key=b13ceb18fc2e77f18ffeab1b28d01a9aab05841742336";
    $headers[] = 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * ' . '/* ';
    $headers[] = 'Accept-Language: zh-cn ';
    $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0';
    $headers[] = 'Host: ' . $host;
    $headers[] = 'Connection: Keep-Alive ';

//    $ret = curl_requests($url,$query_data , $headers);
//    print_r($ret);
    list($resheaders,$body) = curl_requests($url,$query_data , $headers);
    preg_match_all ( "/set\-cookie:([^;]*);/" ,  $resheaders ,  $matches ) ;
//    print_r($matches [ 1 ] [ 0 ]);
//    $cookies   =  substr ( $matches [ 1 ] [ 0 ] ,  0 ) ;
    $cookies =$matches [ 1 ] ;
    $json_obj = json_decode($body,true);
//    echo $json_obj['msg']."</br>";
    return $cookies;
}

function checkin($cookies)
{
//    print_r($cookies)."<br>";
    $url = "https://wangzi.uk/user/checkin";
    $host = "wangzi.uk";
//    print_r( implode("; ", $cookieArr));
//    $cookies = "expire_in=1691521441; uid=22206; key=b13ceb18fc2e77f18ffeab1b28d01a9aab05841742336";
    $headers[] = 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * ' . '/* ';
    $headers[] = 'Accept-Language: zh-cn ';
    $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0';
    $headers[] = 'Host: ' . $host;
    $headers[] = 'Connection: Keep-Alive ';
    $headers[] = 'Cookie: ' . $cookies;
    list($resheaders,$body) = curl_requests($url,array(1), $headers);
//    print_r($body)."<br>";
    $arr = json_decode($body, true);
//    echo $arr['msg']."<br/>";
    return $body;
}

//$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0';
//$query_data = array(
//    "email" => "1587288294@qq.com",
//    "passwd" => "1399092738",
//    "code" => ""
//);
//$url="https://wangzi.uk/auth/login";
//$ret = curl_requests($url,$query_data , $headers);
//print_r( $ret['headers']);
//print_r( json_decode($ret['body'],true));
header('Content-type: application/json');
$cookieArr = login("1587288294@qq.com","1399092738");
$cookies =implode("; ", $cookieArr);
$res = checkin($cookies);
echo $res;
?>
